package cn.seecoder.courselearning.serviceimpl.reply;


import cn.seecoder.courselearning.mapperservice.reply.CommentInboxMapper;
import cn.seecoder.courselearning.mapperservice.reply.ReplyInboxMapper;
import cn.seecoder.courselearning.mapperservice.reply.ReplyMapper;
import cn.seecoder.courselearning.po.reply.InboxItem;
import cn.seecoder.courselearning.po.reply.Reply;
import cn.seecoder.courselearning.po.reply.InboxItem;
import cn.seecoder.courselearning.service.course.CourseService;
import cn.seecoder.courselearning.service.post.PostService;
import cn.seecoder.courselearning.service.reply.ReplyService;
import cn.seecoder.courselearning.service.user.UserService;
import cn.seecoder.courselearning.util.Constant;
import cn.seecoder.courselearning.vo.ResultVO;
import cn.seecoder.courselearning.vo.reply.ReplyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class ReplyServiceImpl implements ReplyService {
    @Resource
    private ReplyMapper replyMapper;
    @Resource
    private CommentInboxMapper commentInboxMapper;
    @Resource
    private ReplyInboxMapper replyInboxMapper;

    private PostService postService;
    private UserService userService;
    private CourseService courseService;
    @Autowired
    public void setPostService(PostService postService){this.postService=postService;}
    @Autowired
    public void setUserService(UserService userService){this.userService=userService;}
    @Autowired
    public void setCourseService(CourseService courseService){this.courseService=courseService;}


    @Override
    public ResultVO<ReplyVO> createComment(ReplyVO replyVO) {
        replyVO.setCreateTime(new Date());
        if(replyVO.getUserId()!=null&&replyVO.getUserId()>0){
            String userName = userService.getUser(replyVO.getUserId()).getUname();
            String courseName = courseService.getCourse(replyVO.getCourseId(),replyVO.getUserId()).getName();
            Reply reply = new Reply(replyVO);
            if(replyMapper.insert(reply)>0){
                if(replyMapper.updateLastReplyTime(replyVO.getReplyingCommentId(),replyVO.getCreateTime())>0){
                    Integer userId=postService.findUserIdByPostId(reply.getReplyingPostId());
                    InboxItem inboxItem = new InboxItem(reply.getId(),userId);
                    if(commentInboxMapper.insert(inboxItem)>0){
                        return new ResultVO<ReplyVO>(Constant.REQUEST_SUCCESS,"创建成功",new ReplyVO(reply,userName,courseName));
                    }
                    return new ResultVO<>(Constant.REQUEST_FAIL,"消息提醒失败");
                }
                return new ResultVO<>(Constant.REQUEST_FAIL,"更新最新回复时间失败");
            }
            return new ResultVO<>(Constant.REQUEST_FAIL,"插入失败");
        }
        return new ResultVO<>(Constant.REQUEST_FAIL,"用户名不存在");
    }

    @Override
    public ResultVO<ReplyVO> createReply(ReplyVO replyVO) {
        replyVO.setCreateTime(new Date());
        if(replyVO.getUserId()!=null&&replyVO.getUserId()>0){
            String userName = userService.getUser(replyVO.getUserId()).getUname();
            String courseName = courseService.getCourse(replyVO.getCourseId(),replyVO.getUserId()).getName();
            Reply reply = new Reply(replyVO);
            if(replyMapper.insert(reply)>0){
                if(replyMapper.updateLastReplyTime(replyVO.getReplyingCommentId(),replyVO.getCreateTime())>0){
                    Integer userId=replyMapper.selectByPrimaryKey(reply.getReplyingCommentId()).getUserId();
                    InboxItem inboxItem = new InboxItem(reply.getId(),userId);
                    if(replyInboxMapper.insert(inboxItem)>0){
                        return new ResultVO<ReplyVO>(Constant.REQUEST_SUCCESS,"创建成功",new ReplyVO(reply,userName,courseName));
                    }
                    return new ResultVO<>(Constant.REQUEST_FAIL,"消息提醒失败");
                }
                return new ResultVO<>(Constant.REQUEST_FAIL,"更新最新回复时间失败");
            }
            return new ResultVO<>(Constant.REQUEST_FAIL,"插入失败");
        }
        return new ResultVO<>(Constant.REQUEST_FAIL,"用户名不存在");
    }

    @Override
    public List<Integer> getCommentNews(Integer userId,Integer lastMaxId) {
        return commentInboxMapper.find(userId,lastMaxId);
    }
    @Override
    public List<Integer> getReplyNews(Integer userId,Integer lastMaxId) {
        return replyInboxMapper.find(userId,lastMaxId);
    }
}
